package com.pricl.frame.query.criteria;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;

public class BaseCriteria implements Serializable {

	private static final long serialVersionUID = 1L;
	protected String orderByClause;
	protected boolean distinct;
	protected List<BaseCriteria> oredCriteria;
	protected List<Criterion> criterions;

	public BaseCriteria() {
		this.oredCriteria = Lists.newArrayList();
		this.criterions = Lists.newArrayList();
		this.oredCriteria.add(this);
	}

	public void setOrderByClause(String orderByClause) {
		this.orderByClause = orderByClause;
	}

	public String getOrderByClause() {
		return this.orderByClause;
	}

	public void setDistinct(boolean distinct) {
		this.distinct = distinct;
	}

	public boolean isDistinct() {
		return this.distinct;
	}

	public List<BaseCriteria> getOredCriteria() {
		return this.oredCriteria;
	}

	public void or(BaseCriteria criteria) {
		this.oredCriteria.add(criteria);
	}

	public void clear() {
		this.oredCriteria.clear();
		this.orderByClause = null;
		this.distinct = false;
	}

	public boolean isValid() {
		return this.criterions.size() > 0;
	}

	public List<Criterion> getAllCriteria() {
		return this.criterions;
	}

	public List<Criterion> getCriterion() {
		return this.criterions;
	}

	protected void addCriterion(String condition) {
		if (StringUtils.isNotBlank(condition))
			this.criterions.add(new Criterion(condition));
	}

	protected void addCriterion(String condition, Object value, String property) {
		if ((StringUtils.isNotBlank(condition)) && (value != null))
			this.criterions.add(new Criterion(condition, value));
	}

	protected void addCriterion(String condition, Object value1, Object value2, String property) {
		if ((value1 != null) && (value2 != null))
			this.criterions.add(new Criterion(condition, value1, value2));
	}
}
